/*
 * @(#)MaintMtmWorkflowProgressNotifyRuleAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.quartz.SchedulerException;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.workflow.WorkflowErrorConstant;
import com.dcivision.workflow.bean.MtmWfStepNotificationRule;
import com.dcivision.workflow.bean.MtmWorkflowProgressNotifyRule;
import com.dcivision.workflow.bean.MtmWorkflowProgressNotifyTarget;
import com.dcivision.workflow.bean.MtmWorkflowProgressPriorityRule;
import com.dcivision.workflow.bean.MtmWorkflowStepUserActor;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.bean.WorkflowStep;
import com.dcivision.workflow.core.WorkflowOperationManager;
import com.dcivision.workflow.core.WorkflowProgressManager;
import com.dcivision.workflow.dao.MtmWorkflowProgressNotifyRuleDAObject;
import com.dcivision.workflow.dao.MtmWorkflowProgressNotifyTargetDAObject;
import com.dcivision.workflow.dao.WorkflowProgressDAObject;
import com.dcivision.workflow.dao.WorkflowStepDAObject;

/**
 * MaintMtmWorkflowStepPriorityRuleAction.java
 * @author      Dick xie
 * @company     DCIVision Limited
 * @creation date   10/1/2004
 * @version     $Revision: 1.11.2.1 $
 */


public class MaintMtmWorkflowProgressNotifyRuleAction extends com.dcivision.framework.web.AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.11.2.1 $";
  public static final String REMINDER_SET_SUCCESSED="workflow.message.reminder_set_successed";

  /** Creates a new instance of MaintMtmWorkflowStepPriorityRuleAction */
  public MaintMtmWorkflowProgressNotifyRuleAction() {
    super();
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return SystemFunctionConstant.WORKFLOW_TASK;
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return "com.dcivision.workflow.dao.MtmWorkflowProgressNotifyRuleDAObject";
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AbstractActionForm maintForm = (AbstractActionForm)form;
    MaintMtmWorkflowProgressNotifyRuleForm progressNotifyForm = (MaintMtmWorkflowProgressNotifyRuleForm)maintForm;

    String opMode = (maintForm != null) ? maintForm.getOpMode() : request.getParameter("opMode"); //maintForm.getOpMode();
    String navMode = (maintForm != null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();
    String arrivedDate = request.getParameter("arrivedDate");
    String dueDate = request.getParameter("dueDate");
    if (Utility.isEmpty(arrivedDate)) {
      progressNotifyForm.setArrivedDate(arrivedDate);
    }
    if (Utility.isEmpty(dueDate)) {
      progressNotifyForm.setDueDate(dueDate);
    }

    // get had notify rule record Form db;
    if (!Utility.isEmpty(request.getParameter("ID")) && !"null".equals(request.getParameter("ID")) && Utility.isEmpty(opMode)) {
      String workflowProgressStr = null;
      try{
        workflowProgressStr = this.getWorkflowProgressNotificationRuleByWorkflowProgressID(new Integer(request.getParameter("ID")), request);
      } catch(ApplicationException ex) {
        log.error(ex, ex);
      }
        progressNotifyForm.setNotificationRuleListValue(workflowProgressStr);
    }

    if (GlobalConstant.OP_MODE_REFRESH.equals(opMode)) {
      // Set the function code for current action.
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward != null) {
        return forward;
      }
      refreshFormData(mapping, maintForm, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_REFRESH);
    } else if (GlobalConstant.OP_MODE_INSERT.equals(opMode)) {
      try {
        this.deleteWorkflowProgressNotificationRule(request, new Integer(progressNotifyForm.getID()));
        this.insertWorkflowProgressNotificationRule(request, maintForm, new Integer(progressNotifyForm.getID()));
        this.createScheduleForProgressNotifiyReminder(new Integer(progressNotifyForm.getID()), this.getSessionContainer(request), this.getConnection(request));
        this.commit(request);
        addMessage(request, REMINDER_SET_SUCCESSED);
        progressNotifyForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
      } catch (ApplicationException ex) {
        log.error(ex, ex);
      }
      return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
    }else {
      return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
    }
  }

  public void refreshFormData(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws
    ApplicationException {
    List resultList = new ArrayList();

    try {
      SessionContainer sessionContainer = this.getSessionContainer(request);
      MaintMtmWorkflowProgressNotifyRuleForm notificationRuleForm = (MaintMtmWorkflowProgressNotifyRuleForm)form;

      String notificationRuleListValue = "";
      String notificationRule = "";
      String[] tmpRuleArr = null;
      String notificationToOwner = null;
      String[] tmpOwnerArr = null;
      if (!Utility.isEmpty(request.getParameter("notificationOwnerListValue"))) {
        notificationToOwner = request.getParameter("notificationOwnerListValue");
      }
      if(!Utility.isEmpty(notificationToOwner)) {
        notificationRule += notificationRuleForm.getNotifyTrigger() + "," +
        notificationRuleForm.getNotifyAtAmt() + "," +
        notificationRuleForm.getNotifyAtField() + "," +
        notificationRuleForm.getNotifyAckMethodByEmail() + "," +
        notificationRuleForm.getNotifyAckMethodBySystem() + "," +
        "1," +
        notificationToOwner;
      } else {
        notificationRule += notificationRuleForm.getNotifyTrigger() + "," +
        notificationRuleForm.getNotifyAtAmt() + "," +
        notificationRuleForm.getNotifyAtField() + "," +
        notificationRuleForm.getNotifyAckMethodByEmail() + "," +
        notificationRuleForm.getNotifyAckMethodBySystem() + "," +
        "1," +
        this.getSessionContainer(request).getUserRecordID().toString();
      }

      if(!Utility.isEmpty(request.getParameter("notificationRuleListValue")) && !"null".equals(request.getParameter("notificationRuleListValue"))){
        notificationRuleListValue = request.getParameter("notificationRuleListValue");
      }

      if(!Utility.isEmpty(notificationRuleListValue)){
        tmpRuleArr = TextUtility.splitString(notificationRuleListValue,"|");
      }

      String[] deleteItemArr = request.getParameterValues("deleteItem");
      boolean checked = false;
      notificationRuleListValue = "";

      for(int j=0; !Utility.isEmpty(tmpRuleArr) &&  j<tmpRuleArr.length; j++){
        checked = false;
        for(int i=0; !Utility.isEmpty(deleteItemArr) && i<deleteItemArr.length; i++){
          if(j == Integer.parseInt(deleteItemArr[i])){
            checked = true;
            break;
          }
        }
        if(!checked){
          if(Utility.isEmpty(notificationRuleListValue)){
            notificationRuleListValue += tmpRuleArr[j];
          }else{
            notificationRuleListValue += "|" + tmpRuleArr[j];
          }
        }
      }

      if(Utility.isEmpty(notificationRuleListValue)){
        notificationRuleListValue = notificationRule;
      } else {
        notificationRuleListValue += "|" + notificationRule;
      }
      notificationRuleForm.setNotificationRuleListValue(notificationRuleListValue);

    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }

    private void insertWorkflowProgressNotificationRule(
          HttpServletRequest request,
          AbstractActionForm form,
          Integer progressID) throws ApplicationException {
        MtmWorkflowProgressNotifyRuleDAObject notificationRuleDAO = new MtmWorkflowProgressNotifyRuleDAObject(this.getSessionContainer(request),this.getConnection(request));
        MtmWorkflowProgressNotifyTargetDAObject notificationTargetDAO = new MtmWorkflowProgressNotifyTargetDAObject(this.getSessionContainer(request),this.getConnection(request));
        MaintMtmWorkflowProgressNotifyRuleForm maintForm = (MaintMtmWorkflowProgressNotifyRuleForm)form;

        String NotifyRuleListValue = maintForm.getNotificationRuleListValue();
        if(!Utility.isEmpty(NotifyRuleListValue)){
          String[] ruleArr = TextUtility.splitString(NotifyRuleListValue, "|");
          log.debug("The notifyRuleList value ...." + ruleArr.length);
          for(int i=0; i<ruleArr.length; i++){
            String[] tmpRuleArr = TextUtility.splitString(ruleArr[i], ",");
            String ackMethod = "";
            MtmWorkflowProgressNotifyRule notificationRule = new MtmWorkflowProgressNotifyRule();

            notificationRule.setWorkflowProgressID(progressID);
            notificationRule.setNotifyTrigger(tmpRuleArr[0]);
            notificationRule.setNotifyAtAmt(TextUtility.parseIntegerObj(tmpRuleArr[1]));
            notificationRule.setNotifyAtField(TextUtility.parseIntegerObj(tmpRuleArr[2]) );

            if(!"null".equals(tmpRuleArr[3])){
              ackMethod = tmpRuleArr[3];
            }
            if(!"null".equals(tmpRuleArr[4])) {
              if(Utility.isEmpty(ackMethod)){
                ackMethod = tmpRuleArr[4];
              } else {
                ackMethod += "," + tmpRuleArr[4];
              }
            }
            notificationRule.setNotifyAckMethod(ackMethod);
            notificationRule = (MtmWorkflowProgressNotifyRule)notificationRuleDAO.insertObject(notificationRule);
            //insert table MTM_WF_PROGRESS_WF_NOTIFICATION_TARGER
            String notificationOwnerStr = tmpRuleArr[6];
            if (!Utility.isEmpty(notificationOwnerStr)) {
              MtmWorkflowProgressNotifyTarget notificationTarget = new MtmWorkflowProgressNotifyTarget();
              notificationTarget.setNotifyRuleID(notificationRule.getID());
              notificationTarget.setTargetType(MtmWorkflowStepUserActor.ACTOR_TYPE_USER);
              notificationTarget.setTargetID(new Integer(notificationOwnerStr));
              notificationTargetDAO.insertObject(notificationTarget);
            }
          }
       }
    }

    /**
     * Add By Dick
     * TODO
     * @param request
     * @param stepID
     * @throws ApplicationException
     */
  private void deleteWorkflowProgressNotificationRule(
      HttpServletRequest request,
      Integer progressID) throws ApplicationException {
    MtmWorkflowProgressNotifyRuleDAObject notificationRuleDAO = new MtmWorkflowProgressNotifyRuleDAObject(this.getSessionContainer(request),this.getConnection(request));
    MtmWorkflowProgressNotifyTargetDAObject notificationTargetDAO = new MtmWorkflowProgressNotifyTargetDAObject(this.getSessionContainer(request),this.getConnection(request));

    List notificationRuleList = notificationRuleDAO.getListByWorkflowProgressID(progressID);
    for(int i=0; i<notificationRuleList.size(); i++){
      MtmWorkflowProgressNotifyRule notificationRule = (MtmWorkflowProgressNotifyRule)notificationRuleList.get(i);
      List notificationTargetList = notificationTargetDAO.getListByNotifyRuleID(notificationRule.getID());
      for (int j = 0; j < notificationTargetList.size(); j++) {
        MtmWorkflowProgressNotifyTarget  currentTarget = (MtmWorkflowProgressNotifyTarget)notificationTargetList.get(j);
        String jobName = GlobalConstant.JOB_PREFIX_WORKFLOW_NOTIFY + notificationRule.getNotifyTrigger() +  "_" + progressID.toString() + "_" + notificationRule.getID().toString();
        String jobGroup = GlobalConstant.JOB_PREFIX_WORKFLOW_NOTIFY+"GROUP";
        try {
          log.debug("=== Cancel Schedule_Job ===");
          org.quartz.Scheduler sched = com.dcivision.framework.SchedulerFactory.getScheduler();
          sched.unscheduleJob( jobName, jobGroup);
        } catch (SchedulerException se) {
          log.debug("=== Cancel Schedule_Job Fail ===");
          throw new ApplicationException(WorkflowErrorConstant.CANCEL_SCHEDULE_JOB_FAIL);
        }
        notificationTargetDAO.delete(currentTarget);
      }
      notificationRuleDAO.delete(notificationRule);
    }
  }

  private String getWorkflowProgressNotificationRuleByWorkflowProgressID(Integer workflowProgressID, HttpServletRequest request)throws ApplicationException {
    MtmWorkflowProgressNotifyRuleDAObject notificationRuleDAO = new MtmWorkflowProgressNotifyRuleDAObject(this.getSessionContainer(request),this.getConnection(request));
    MtmWorkflowProgressNotifyTargetDAObject notificationTargetDAO = new MtmWorkflowProgressNotifyTargetDAObject(this.getSessionContainer(request),this.getConnection(request));
    WorkflowProgressDAObject workflowStepDAO = new WorkflowProgressDAObject(this.getSessionContainer(request),this.getConnection(request));
    String notificationRuleListValue = "";
    List notificationRuleList = notificationRuleDAO.getListByWorkflowProgressID(workflowProgressID);

    for (int i=0; !Utility.isEmpty(notificationRuleList) && i < notificationRuleList.size(); i++) {
      MtmWorkflowProgressNotifyRule  notificationRule = (MtmWorkflowProgressNotifyRule)notificationRuleList.get(i);
      String tmpRule = "";
      String ackMethod = "";

      if(notificationRule.getNotifyAckMethod().indexOf(MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL) >-1){
        ackMethod = MtmWorkflowProgressPriorityRule.NOTIFY_BY_EMAIL;
      } else{
        ackMethod = "null";
      }

      if(notificationRule.getNotifyAckMethod().indexOf(MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM) >-1){
        ackMethod += "," + MtmWorkflowProgressPriorityRule.NOTIFY_BY_SYSTEM;
      } else{
        ackMethod += "," + "null";
      }

      tmpRule = notificationRule.getNotifyTrigger() + "," +
                notificationRule.getNotifyAtAmt() + "," +
                notificationRule.getNotifyAtField() + "," +
                ackMethod;
      if (this.checkSchendar(request, workflowProgressID, notificationRule)) {
        tmpRule += "," + "1";
      } else {
        tmpRule += "," + "0";
      }
      List currentTargetList = notificationTargetDAO.getListByNotifyRuleID(notificationRule.getID());

      String currentOwners = "";
      if (!Utility.isEmpty(currentTargetList)) {
        for (int h = 0; h < currentTargetList.size(); h++) {
          MtmWorkflowProgressNotifyTarget currentTarget = (MtmWorkflowProgressNotifyTarget)currentTargetList.get(h);
          tmpRule += "," + currentTarget.getTargetID();
        }

      }
      if(i==0){
        notificationRuleListValue = tmpRule;
      } else {
        notificationRuleListValue += "|" + tmpRule;
      }
    }
    log.debug("this notificationRuleListValue is ::" + notificationRuleListValue);
    return notificationRuleListValue;
  }

  private void createScheduleForProgressNotifiyReminder(
          Integer wfProgressID,
          SessionContainer sessionConstainer,
          Connection dbConn) throws ApplicationException {
     MtmWorkflowProgressNotifyRuleDAObject notificationRuleDAO = new MtmWorkflowProgressNotifyRuleDAObject(sessionConstainer, dbConn);
     MtmWorkflowProgressNotifyTargetDAObject notificationTargetDAO = new MtmWorkflowProgressNotifyTargetDAObject(sessionConstainer, dbConn);
     WorkflowOperationManager workflowOperationManager = new WorkflowOperationManager(sessionConstainer, dbConn);
     WorkflowProgressManager workflowProgressManager = new WorkflowProgressManager(sessionConstainer, dbConn);
     WorkflowStepDAObject stepDAO = new WorkflowStepDAObject(sessionConstainer, dbConn);
     WorkflowProgressDAObject progressDAO = new WorkflowProgressDAObject(sessionConstainer, dbConn);

     WorkflowProgress wfProgress = (WorkflowProgress)progressDAO.getObjectByID(wfProgressID);
     WorkflowStep wfStep = (WorkflowStep)stepDAO.getObjectByID(wfProgress.getWorkflowStepID());
     WorkflowProgress workflowProgress = new WorkflowProgress();

     Timestamp dueDate = wfProgress.getDueDate();
     Timestamp arrivedDate = null;


     //get the arrived Date
     arrivedDate = wfProgress.getCreateDate();
       List stepNotificationRuleList = notificationRuleDAO.getListByWorkflowProgressID(wfProgressID);
       log.debug("the StepNotificationRuleList length " + stepNotificationRuleList.size());
       for (int j = 0; j < stepNotificationRuleList.size(); j++ ) {
         MtmWorkflowProgressNotifyRule progressNotificationRule = (MtmWorkflowProgressNotifyRule)stepNotificationRuleList.get(j);
         //List stepNotificationTargetList = notificationTargetDAO.getListByNotifyRuleID(progressNotificationRule.getID());
         //for (int k = 0; k < stepNotificationTargetList.size(); k++) {
          // MtmWorkflowProgressNotifyTarget progressNotificationTarget = (MtmWorkflowProgressNotifyTarget)stepNotificationTargetList.get(k);
           Calendar triggerCal = null;
           if(MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_ARRIVED.equals(progressNotificationRule.getNotifyTrigger()) && WorkflowProgress.STATUS_PENDING.equals(wfProgress.getStatus())){
             log.debug("Trigger Type Is After Task Arrived Notification.");
             triggerCal = Utility.timestampToCalendar(arrivedDate);
             if((new Integer(Calendar.DATE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.DATE, progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.HOUR)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.HOUR, progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.MINUTE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.MINUTE, progressNotificationRule.getNotifyAtAmt().intValue());
             }
           }
           if(MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_PENDING_FOR.equals(progressNotificationRule.getNotifyTrigger()) && WorkflowProgress.STATUS_PENDING.equals(wfProgress.getStatus())){
             log.debug("Trigger Type Is After Task is pending for  Notification.");
             triggerCal = Utility.timestampToCalendar(arrivedDate);
             if((new Integer(Calendar.DATE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.DATE, progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.HOUR)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.HOUR, progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.MINUTE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.MINUTE, progressNotificationRule.getNotifyAtAmt().intValue());
             }
           }
            //Befor due date notification.
           if(MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_BEFORE_DUE_DATE.equals(progressNotificationRule.getNotifyTrigger())){
             log.debug("Trigger Type Is Befor Due Date Notification.");
             if(Utility.isEmpty(dueDate)){
               break;
             }
             triggerCal = Utility.timestampToCalendar(dueDate);
             if((new Integer(Calendar.DATE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.DATE, - progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.HOUR)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.HOUR, - progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.MINUTE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.MINUTE, - progressNotificationRule.getNotifyAtAmt().intValue());
             }
           }
           if(MtmWfStepNotificationRule.NOTIFY_TRIGGER_TYPE_OVER_DUE_DATE.equals(progressNotificationRule.getNotifyTrigger())){
             log.debug("Trigger Type Is over Due Date Notification.");
             if(Utility.isEmpty(dueDate)){
               break;
             }
             triggerCal = Utility.timestampToCalendar(dueDate);
             if((new Integer(Calendar.DATE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.DATE, progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.HOUR)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.HOUR, progressNotificationRule.getNotifyAtAmt().intValue());
             } else if((new Integer(Calendar.MINUTE)).equals(progressNotificationRule.getNotifyAtField()) ){
               triggerCal.add(Calendar.MINUTE, progressNotificationRule.getNotifyAtAmt().intValue());
             }
           }
           if (!Utility.isEmpty(triggerCal)) {
             triggerCal.add(Calendar.MINUTE, 1);
             workflowProgressManager.createScheduleJobForNotificationRule(wfProgress, wfStep,  sessionConstainer.getUserRecordID(), Utility.calendarToTimestamp(triggerCal),progressNotificationRule);
           }
         }
       }
     //}

  private boolean checkSchendar(
      HttpServletRequest request,
      Integer progressID,
      MtmWorkflowProgressNotifyRule  notificationRule)throws ApplicationException {
    MtmWorkflowProgressNotifyTargetDAObject notificationTargetDAO = new MtmWorkflowProgressNotifyTargetDAObject(this.getSessionContainer(request),this.getConnection(request));
    List notificationTargetList = notificationTargetDAO.getListByNotifyRuleID(notificationRule.getID());
    boolean isHaveSchendar = true;
    String triggerName = GlobalConstant.JOB_PREFIX_WORKFLOW_NOTIFY + notificationRule.getNotifyTrigger() + "_" + progressID.toString() + "_" + notificationRule.getID().toString();
    String triggerGroup = GlobalConstant.JOB_PREFIX_WORKFLOW_NOTIFY+"GROUP";
    log.debug("this workflowTrigger is::::" + triggerName + "::" + triggerGroup);
    try {
      org.quartz.Scheduler sched = com.dcivision.framework.SchedulerFactory.getScheduler();
      org.quartz.Trigger trigger = sched.getTrigger(triggerName, triggerGroup);

      log.debug("=== Cancel Schedule_Job ===");
      if(Utility.isEmpty(trigger)) {
        isHaveSchendar = false;
      }
    } catch (Exception se) {
      log.debug("=== Cancel Schedule_Job Fail ===");
      throw new ApplicationException(WorkflowErrorConstant.CANCEL_SCHEDULE_JOB_FAIL);
    }
    return isHaveSchendar;
  }

}
